/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralelements;

import org.openaecfm.model.structuralelements.ReinforcingElement;
import org.openaecfm.model.structuralelements.ReinforcingMesh;
import org.openaecfm.model.structuralelements.ReinforcingMeshTypeEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class ReinforcingMeshIO extends
		AbstractEntityInstanceInputImpl<ReinforcingMesh> implements
		IEntityInstanceOutput<ReinforcingMesh> {
	private IEntityInstanceUtilInput<ReinforcingElement> utilInput;
	private IEntityInstanceUtilOutput<ReinforcingElement> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<ReinforcingElement> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(ReinforcingMesh pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCREINFORCINGMESH", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set MeshLength
		if (pojo.getMeshLength() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getMeshLength()));
		}

		// Set MeshWidth
		if (pojo.getMeshWidth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getMeshWidth()));
		}

		// Set LongitudinalBarNominalDiameter
		if (pojo.getLongitudinalBarNominalDiameter() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo
					.getLongitudinalBarNominalDiameter()));
		}

		// Set TransverseBarNominalDiameter
		if (pojo.getTransverseBarNominalDiameter() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo
					.getTransverseBarNominalDiameter()));
		}

		// Set LongitudinalBarCrossSectionArea
		if (pojo.getLongitudinalBarCrossSectionArea() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo
					.getLongitudinalBarCrossSectionArea()));
		}

		// Set TransverseBarCrossSectionArea
		if (pojo.getTransverseBarCrossSectionArea() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo
					.getTransverseBarCrossSectionArea()));
		}

		// Set LongitudinalBarSpacing
		if (pojo.getLongitudinalBarSpacing() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLongitudinalBarSpacing()));
		}

		// Set TransverseBarSpacing
		if (pojo.getTransverseBarSpacing() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTransverseBarSpacing()));
		}

		// Set PredefinedType
		if (pojo.getPredefinedType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ReinforcingMesh doCreateObject() {
		ReinforcingMesh pojo = new ReinforcingMesh();

		return pojo;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			ReinforcingMesh pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<ReinforcingElement> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ReinforcingMesh pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set MeshLength
		Parameter meshlength = sei.getParameter(9);
		if (meshlength.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) meshlength;
			pojo.setMeshLength(r.toDouble());
		}

		// Set MeshWidth
		Parameter meshwidth = sei.getParameter(10);
		if (meshwidth.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) meshwidth;
			pojo.setMeshWidth(r.toDouble());
		}

		// Set LongitudinalBarNominalDiameter
		Parameter lbnd = sei.getParameter(11);
		if (lbnd.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lbnd;
			pojo.setLongitudinalBarNominalDiameter(r.toDouble());
		}

		// Set TransverseBarNominalDiameter
		Parameter tbnd = sei.getParameter(12);
		if (tbnd.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tbnd;
			pojo.setTransverseBarNominalDiameter(r.toDouble());
		}

		// Set LongitudinalBarCrossSectionArea
		Parameter lbca = sei.getParameter(13);
		if (lbca.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lbca;
			pojo.setLongitudinalBarCrossSectionArea(r.toDouble());
		}

		// Set TransverseBarCrossSectionArea
		Parameter tbca = sei.getParameter(14);
		if (tbca.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tbca;
			pojo.setTransverseBarCrossSectionArea(r.toDouble());
		}

		// Set LongitudinalBarSpacing
		Parameter lbs = sei.getParameter(15);
		if (lbs.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) lbs;
			pojo.setLongitudinalBarSpacing(r.toDouble());
		}

		// Set TransverseBarSpacing
		Parameter tbs = sei.getParameter(16);
		if (tbs.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tbs;
			pojo.setTransverseBarSpacing(r.toDouble());
		}

		// Set PredefinedType
		Parameter predefinedType = sei.getParameter(17);
		if (predefinedType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) predefinedType;
			pojo.setPredefinedType(ReinforcingMeshTypeEnum.valueOf(e
					.toEnumString()));
		}
	}
}
